package com.jnxx.okr.controller;

import com.jnxx.basic.query.PageInfo;
import com.jnxx.basic.util.AjaxResult;
import com.jnxx.okr.domain.Okr;
import com.jnxx.okr.query.OkrQuery;
import com.jnxx.okr.service.IOkrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/api/okr")
public class OkrController {

    @Autowired
    public IOkrService okrService;

    /**
     * 接口：添加或修改
     * @param okrMap  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Map<String, Okr> okrMap){
        Okr okr = okrMap.get("okr");
        if( okr.getId()!=null){
		okrService.update(okr);
        }else{
                okrService.add(okr);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        okrService.del(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        okrService.batchDel(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Okr findOne(@PathVariable("id")Long id) {
        return okrService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping("/test")
    public List<Okr> findAll(){
        return okrService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageInfo<Okr> page(@RequestBody OkrQuery query) {
        return okrService.page(query);
    }

    /**
     * 通过员工id获取他的okr
     * @return
     */
    @GetMapping("/getOkrByStaffId/{cycleId}")
    public List<Okr> getOkrByStaffId(@PathVariable("cycleId") Long cycleId) {
        return okrService.getOkrByStaffId(cycleId);
    }

    /**
     * 通过员工id获取他的okr按周期分类
     * @return
     */
    @GetMapping("/getOkrByStaffIdGroupByCycle/{staffId}")
    public Map<Long, List<Okr>> getOkrByStaffIdGroupByCycle(@PathVariable("staffId") Long staffId) {
        return okrService.getOkrByStaffIdGroupByCycle(staffId);
    }



    /**
     * 通过okrId获取okr的详细信息
     * @param id
     * @return
     */
    @GetMapping("/getOkrDetailById/{id}")
    public Okr getOkrDetailById(@PathVariable("id") Long id){
        return okrService.getOkrDetailById(id);
    }

    /**
     * 根据周期id获取所有okr
     * @param
     * @return
     */
    @GetMapping("/getAllOkrGroupBycycleId")
    public Map<Long, List<Okr>> getOkrBycycleId(){
        return okrService.getOkrBycycleId();
    }

    /**
     * 修改OKR进度
     * @param okrMap
     * @return
     */
    @PutMapping("/updateProgress")
    public AjaxResult updateProgress(@RequestBody Map<String, Okr> okrMap){
        Okr okr = okrMap.get("okr");
        okrService.updateProgress(okr);
        return new AjaxResult();
    }

    @GetMapping("/getKrDetailById/{id}")
    public Okr getKrDetailById(@PathVariable("id")Long id) {
        return okrService.getKrDetailById(id);
    }

    /**
     * 获取当前人负责的okr
     * @return
     */
    @GetMapping("/findOkrByLeader/{leaderId}")
    public List<Okr> findOkrByLeader(@PathVariable("leaderId")Long leaderId){

        return okrService.findOkrByLeader(leaderId);
    }
}
